package com.fire.control.system.module.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;
import com.fire.control.system.module.company.dao.ManagementRepository;
import com.fire.control.system.module.system.dao.ResourceRepository;
import com.fire.control.system.module.system.dao.RoleRepository;
import com.fire.control.system.module.system.service.InitSystemService;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.comman.SimpleRequestProtocol;
import com.fire.control.system.support.util.BusinessStaticValue;
import com.fire.control.system.support.util.InitSystemUtil;

@Service
public class InitSystemServiceImpl implements InitSystemService{
	
	@Autowired private ProtocolSelector protocolSelector;
	
	@Autowired private ResourceRepository resourceRepository;
	
	@Autowired private ManagementRepository companyRepository;
	
	@Autowired private RoleRepository roleRepository;
	
	@Override
	public ResponseProtocol initResources(SimpleRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		if(this.validateLogin(requestProtocol, response) == false){
			return response;
		}
		resourceRepository.save(InitSystemUtil.getInitAllResources());
		response.setIsSuccess(true);
		response.setMessage("初始化系统资源成功！");
		return response;
	}
	@Override
	public ResponseProtocol initSuperCompany(SimpleRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		if(this.validateLogin(requestProtocol, response) == false){
			return response;
		}
		Role role = roleRepository.findOne(BusinessStaticValue.init_role_id);
		if(role == null){
			response.setIsSuccess(false);
			response.setMessage("初始化系统超级公司失败：没有可用的角色！");
			return response;
		}
		List<Role> roles = new ArrayList<Role>();
		roles.add(role);
		companyRepository.save(InitSystemUtil.getInitSuperCompany(roles));
		response.setIsSuccess(true);
		response.setMessage("初始化系统超级公司成功！");
		return response;
	}
	
	private boolean validateLogin(SimpleRequestProtocol requestProtocol,ResponseProtocol response){
		if(requestProtocol.getLoginName().equals(BusinessStaticValue.init_system_loginName) == false
				|| requestProtocol.getPassword().equals(BusinessStaticValue.init_system_password) == false){
			response.setIsSuccess(false);
			response.setMessage("登录名或者密码错误!");
			return false;
		}
		return true;
	}
	@Override
	public ResponseProtocol initRoles(SimpleRequestProtocol requestProtocol) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		if(this.validateLogin(requestProtocol, response) == false){
			return response;
		}
		List<Resource> resources = resourceRepository.findAll();
		roleRepository.save(InitSystemUtil.getInitSuperRole(resources));
		response.setIsSuccess(true);
		response.setMessage("初始化系统角色成功！");
		return response;
	}
}
